﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using QD.Web.Upload;

namespace QD.Web.Upload
{
    [XmlRoot("UploadSetting")]
    public class UploadSetting
    {
        public UploadSetting()
        {
            _items = new List<UploadFileConfig>();
            _items2 = new List<UploadImageConfig>();
        }
        List<UploadFileConfig> _items = null;


        [XmlElement("FileConfig")]
        public UploadFileConfig[] FileItems
        {
            get { return _items.ToArray(); }
            set
            {
                _items.AddRange(value);
            }
        }

        public void Add(UploadFileConfig item)
        {
            if (item is UploadImageConfig)
                this._items2.Add(item as UploadImageConfig);
            else
                this._items.Add(item);
        }

        List<UploadImageConfig> _items2 = null;
        [XmlElement("ImageConfig")]
        public UploadImageConfig[] ImageItems
        {
            get { return _items2.ToArray(); }
            set
            {
                _items2.AddRange(value);
            }
        }


        public void SerializeObject(string filename)
        {
            XmlSerializer s = new XmlSerializer(typeof(UploadSetting));

            TextWriter writer = new StreamWriter(filename);
            s.Serialize(writer, this);
            writer.Close();
        }

        public static UploadSetting DeserializeObject(string filename)
        {
            FileStream fs = new FileStream(filename, FileMode.Open);
            XmlSerializer x = new XmlSerializer(typeof(UploadSetting));
            Object target = x.Deserialize(fs);
            return target as UploadSetting;
        }
    }
}
